'''
    @author : walker
    @time : 2019/10/29
    @description : 对各项数据进行统计并画图用用途
'''
import seaborn as sns
import matplotlib.pyplot as plt
from config import *
from get_micro_search_data import *
import numpy as np
from scipy.stats import pearsonr

#设置画图的字体
plt.style.use("fivethirtyeight")
sns.set_style({'font.sans-serif':['simhei','Arial']})

class Statistic_field_and_people(object):
    '''
        1、统计所有paper中含有多少个领域,多少个作者
        2、对上面得到的数据进行画图处理
        3、统计每个顶级领域出现的次数
    '''
    def __init__(self):
        # self.paper_data = paper_data
        pass

    # @staticmethod
    def statistic_field_and_people_number(self):
        '''
            统计所有paper中含有多少个领域,多少个作者
            param:
                None
            return:
                people_number : 论文中的人数总数
                field_number : 论文中出现的领域总数
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({}).batch_size(1)

        people_number = 0
        people_number_dumplication = []
        people_number_dumplication_number = 0
        field_number = 0
        field_number_dumplication = []
        field_dumplication_number = 0

        male_number = 0
        female_number = 0
        male_dumplication_number = 0
        female_dumplication_number = 0
        #这里可以用多线程去做
        for paper_item in paper_data:
            #获取到dumplication_paper_info中有多少人
            if paper_item.get("AA"):
                for AA_item in paper_item.get("AA"):
                    people_number += 1
                    if AA_item.get("Sex") == 1:
                        male_number += 1
                    if AA_item.get("Sex") == 0:
                        female_number += 1

                    if AA_item.get("AuId") not in people_number_dumplication:
                        if AA_item.get("Sex") == 1:
                            male_dumplication_number += 1
                        if AA_item.get("Sex") == 0:
                            female_dumplication_number += 1
                        people_number_dumplication_number += 1
                        people_number_dumplication.append(AA_item.get("AuId"))
            #获取到dumplication_paper_info中有多少领域
            if paper_item.get("F"):
                for field_item in paper_item["F"]:
                     field_number += 1
                     if field_item["FId"] not in field_number_dumplication:
                         field_dumplication_number += 1
                         field_number_dumplication.append(field_item["FId"])

        #1832212
        print("论文中总的人数是---------------------：",people_number)
        print("论文中总的男性作者人数是---------------------：",male_number)
        print("论文中总的女性作者人数是---------------------：",female_number)

        print("论文中删除了重复作者的总人数是---------------------：",people_number_dumplication_number)
        print("论文中删除了重复作者的男性人数是---------------------：",male_dumplication_number)
        print("论文中删除了重复作者的女性人数是---------------------：",female_dumplication_number)

        #2506988
        print("论文中总的领域是---------------------：",field_number)
        print("论文中删除了重复领域的总的领域是---------------------：",field_dumplication_number)


        return people_number,field_number
    def pic_field_and_people_number(self):
        '''
            对统计好的paper_number和field_number进行画图操作
            param:
                None
            return:
                pic ：绘制好的图
        '''
        #paper_number的数据，这个可以直接从数据库中得到结果
        paper_number = 280113
        # 获取到论文中的作者数量和领域数量
        # people_number,field_number = self.statistic_field_and_people_number()
        #得到之后将其设为固定值
        people_number = 1832212
        field_number = 2506988

    def get_dumplication_researcher_data(self):
        '''
            存储所有的去重后的paper中的领域和作者信息
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({}).batch_size(1)

        for paper_item in paper_data:
            print(111111)
            if paper_item.get("AA"):
                for AA_item in paper_item.get("AA"):
                    db_data_info['dumplication_paper_info_all_researcher_data'].save(AA_item)
            if paper_item.get("F"):
                for F_item in paper_item.get("F"):
                    db_data_info['dumplication_paper_info_all_field_data'].save(F_item)

    def statistic_top_field(self):
        '''
            统计论文中存在有top_field_name字段的数据，如果不存在这个数据
            代表着这篇论文数据中没有F这一字段，因此我们得不到它最终的顶级领域
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({}).batch_size(1)

        #顶级领域的数目
        top_field_number = 0
        not_have_top_field_number = 0

        # 下面是进行统计工作，统计好之后我们选择直接定义为固定值
        # top_field_dict = {'Biology': 0, 'Physics': 0, 'Materials science': 0, 'Chemistry': 0,
        # 'Environmental science': 0, 'Mathematics': 0, 'Computer science': 0, 'Psychology': 0,
        # 'Sociology': 0, 'Geology': 0, 'Political science': 0, 'Geography': 0, 'Medicine': 0, 'History': 0,
        # 'Engineering': 0, 'Philosophy': 0, 'Economics': 0, 'Art': 0, 'Business': 0}
        # for paper_item in paper_data:
        #     if paper_item.get("top_field_name"):
        #         top_field_number += 1
        #         top_field_dict[paper_item.get("top_field_name")] += 1
        #     else:
        #         not_have_top_field_number += 1
        #
        # print("top_field_number--------",top_field_number)
        # print("not_have_top_field_number---------------",not_have_top_field_number)
        # print(top_field_dict)

        top_field_number = 279831
        not_have_top_field_number = 282
        top_field_dict = {'Biology': 35878, 'Physics': 19513, 'Materials science': 40658, 'Chemistry': 51547,
        'Environmental science': 1086, 'Mathematics': 18533, 'Computer science': 57815, 'Psychology': 3024,
        'Sociology': 783, 'Geology': 6927, 'Political science': 380, 'Geography': 96, 'Medicine': 22273,
        'History': 29, 'Engineering': 14757, 'Philosophy': 17, 'Economics': 6315, 'Art': 11, 'Business': 189}

class Statistic_researcher_rank_info(object):
    '''
        获取所有论文中男女研究者在作者排序上的数据
    '''
    def __init__(self):
        # self.paper_data = paper_data
        pass
    def save_researcher_rank_data(self):
        '''
            存取所有位置男女研究者在排序中的数据
        '''
        for i in range(1,502):
            print(i)
            male_number = 0
            female_number = 0
            sum_number = 0
            researcher_data = db_data_info["dumplication_paper_info_all_researcher_data"].find({"S":i})
            for researcher_item in researcher_data:
                if researcher_item.get("Sex") == 0:
                    female_number += 1
                if researcher_item.get("Sex") == 1:
                    male_number += 1
                sum_number += 1

            db_data_info["dumplication_paper_info_all_researcher_rank_data"].save({"rank":i,"male_number":male_number,"female_number":female_number,"sum_number":sum_number})


    def get_researcher_rank_data(self):
        '''
            获取所有位置男女研究者在排序中的数据
        '''
        researcher_rank_data = []
        #先获取到前十位的rank信息
        researcher_data = db_data_info["dumplication_paper_info_all_researcher_rank_data"].find({}).skip(0).limit(15)
        for researcher_item in researcher_data:
            list = []
            list.append(researcher_item.get("rank"))
            list.append(researcher_item.get("male_number"))
            list.append((researcher_item.get("male_number")/researcher_item.get("sum_number"))*100)
            list.append(researcher_item.get("female_number"))
            list.append((researcher_item.get("female_number")/researcher_item.get("sum_number"))*100)
            list.append(researcher_item.get("sum_number"))
            researcher_rank_data.append(list)

        #获取到十位之后的researcher rank data
        researcher_data = db_data_info["dumplication_paper_info_all_researcher_rank_data"].find({}).skip(15).limit(500)
        list = []
        male_number = 0
        female_number = 0
        sum_number = 0
        for researcher_item in researcher_data:
            # print(researcher_item)
            male_number += researcher_item.get("male_number")
            female_number += researcher_item.get("female_number")
            sum_number += researcher_item.get("sum_number")

        list.append(16)
        list.append(male_number)
        list.append((male_number/sum_number)*100)
        list.append(female_number)
        list.append((female_number/sum_number)*100)
        list.append(sum_number)

        researcher_rank_data.append(list)
        # print(list)
        one = 0
        two = 0
        three = 0
        for researcher_rank_data_item in researcher_rank_data:
            print(researcher_rank_data_item)
            one += researcher_rank_data_item[1]
            two += researcher_rank_data_item[3]
            three += researcher_rank_data_item[5]

        print(one)
        print(two)
        print(three)

        researcher_rank_data = np.array(researcher_rank_data)
        print(researcher_rank_data)
        col_data = researcher_rank_data[:,4]
        print(col_data)

class Statistic_multi_author_number_and_cooperation_model():
    '''
        统计多作者和单个作者的论文数量分布
        统计跨领域论文数量的函数
        获取到作者的合作模式
    '''
    def __init__(self):
        pass

    def get_all_field_paper_and_sex_info(self):
        '''
            获取到所有顶级领域论文的数量和这些论文中男女研究者的数量及其占比数据
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({})

        field_data = []

        for list_item in FIELD_ORI_LIST:
            field_data_item = []
            # field_data_item.append(list_item) #存放领域名称
            field_data_item.append(0)   #存放该领域的paper number
            field_data_item.append(0)   #存放男性作者number
            # field_data_item.append(0)   #存放男性作者 比例
            field_data_item.append(0)   #存放女性作者number
            # field_data_item.append(0)   #存放女性作者比例
            #初始化整个存储所有顶级领域论文的数量和这些论文中男女研究者的数量及其占比数据的二维数组
            field_data.append(field_data_item)

        for paper_item in paper_data:
            #统计每个领域男女研究者数量
            male_number = 0
            female_number = 0
            if paper_item.get("AA"):
                for aa_item in paper_item.get("AA"):
                    #如果是男性
                    if aa_item.get("Sex") == 1:
                        male_number += 1
                    elif aa_item.get("Sex") == 0:
                        female_number += 1
                    else:
                        print("有其他性别出现-----------",aa_item.get("Sex"))

            #统计每篇论文可以划分到哪些顶级领域的数据集里面去
            if paper_item.get("top_field_name_list"):
                for top_field_name_item in paper_item.get("top_field_name_list"):
                    #找到论文中的领域的下标
                    # print(top_field_name_item)
                    for k,v in top_field_name_item.items():
                        # print("K-------------------",k)
                        field_index = FIELD_ORI_LIST.index(k)
                        #找到领域对应的下标位置后将这篇文章的数量加一
                        field_data[field_index][0] += 1
                        field_data[field_index][1] += male_number
                        field_data[field_index][2] += female_number

        list = []
        field_data = np.array(field_data)
        # print(field_data[:,0])

        # list.append(sum(field_data[:,0]))
        # list.append(sum(field_data[:,1]))
        # list.append(sum(field_data[:,2]))
        #
        # list = np.array(list)
        #
        # field_data = np.row_stack((field_data,list))

        col_list_male = []
        col_list_female = []
        for field_data_item in field_data:
            # field_data_item = list(field_data_item)
            # print(field_data_item)
            #男性所占比例
            # field_data_item.append(float(field_data_item[1]/(field_data_item[1] + field_data_item[2])))
            field_data_item = np.append(field_data_item,round(float(field_data_item[1]/(field_data_item[1] + field_data_item[2])),2))
            col_list_male.append(round(float(field_data_item[1]/(field_data_item[1] + field_data_item[2])),2))
            # 女性所占比例
            # field_data_item.append(float(field_data_item[2]/(field_data_item[1] + field_data_item[2])))
            field_data_item = np.append(field_data_item,round(float(field_data_item[2]/(field_data_item[1] + field_data_item[2])),2))
            col_list_female.append(round(float(field_data_item[2]/(field_data_item[1] + field_data_item[2])),2))
            print(field_data_item)

        #将男女占比这个加入到该np数据中
        print("~~~~~~~~~~~")
        col_list_male = np.array(col_list_male)
        col_list_female = np.array(col_list_female)

        field_data = np.column_stack((field_data,col_list_male))
        field_data = np.column_stack((field_data,col_list_female))

        print(np.mean(field_data,axis=0))

    def get_multi_author_number(self):
        '''
            获取到多作者和单个作者的论文数量分别为多少
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({}).batch_size(1)

        sigle_authorship = 0
        multi_author_ship = 0

        for paper_item in paper_data:
            #获取到dumplication_paper_info中有多少人
            if paper_item.get("AA"):
                if len(paper_item.get("AA")) != 1:
                    multi_author_ship += 1
                else:
                    sigle_authorship +=1
        # 4647
        print(sigle_authorship)
        # 275466
        print(multi_author_ship)

    def get_multi_field_paper_data(self):
        '''
            统计跨领域论文数量的函数
        '''
        paper_data = db_data_info["dumplication_paper_info"].find({}).batch_size(1)
        sigle_field_paper = 0
        multi_field_paper = 0
        none_field_paper = 0

        for paper_item in paper_data:
            if paper_item.get("top_field_name_list"):
                if len(paper_item.get("top_field_name_list")) > 1:
                    multi_field_paper += 1
                else:
                    print(len(paper_item.get("top_field_name_list")))
                    sigle_field_paper += 1
            else:
                print(paper_item.get("top_field_name_list"))
                none_field_paper += 1

        # 8567
        print(sigle_field_paper)
        # 271228
        print(multi_field_paper)
        #318
        print(none_field_paper)

class Statistic_first_author_data():
    '''
        统计论文第一个作者是男性或女性的情况
        1.统计男性于女性的占比
        2.论文引用量
        3.跨学科数量
        4.跨学校数量
    '''
    def __init__(self):
        pass

    def get_all_data(slef):

        male_field_data = np.zeros((19,8))
        female_field_data = np.zeros((19,8))

        paper_data = db_data_info["dumplication_paper_info"].find({})
        for paper_item in paper_data:
            #跨领域数量
            top_field_name_list_number = 0
            if paper_item.get("top_field_name_list"):
                top_field_name_list_number = len(paper_item.get("top_field_name_list"))
            #被引次数
            h_index_number = paper_item.get("CC")
            # print(paper_item.get("AA")[0])
            #跨学校的list
            school_list = []
            #男性作者的人数
            male_number = 0
            #女性作者的人数
            female_number = 0

            #定义所有成员男女性的数量
            all_male_number = 0
            all_female_number = 0

            for aa_item in paper_item.get("AA"):
                school_list.append(aa_item.get("AfN"))
            school_list_number = len(list(set(school_list)))

            #判断论文作者的人数超过0的时候
            if len(paper_item.get("AA")) != 0:

                for aa_item in paper_item.get("AA")[1:]:
                    if aa_item.get("Sex") == 1:
                        male_number += 1
                    if aa_item.get("Sex") == 0:
                        female_number += 1

                for aa_item in paper_item.get("AA")[:]:
                    if aa_item.get("Sex") == 1:
                        all_male_number += 1
                    if aa_item.get("Sex") == 0:
                        all_female_number += 1

            if paper_item.get("top_field_name_list"):
                for top_field_name_item in paper_item.get("top_field_name_list"):
                    #找到论文中的领域的下标
                    # print(top_field_name_item)
                    #当男性为第一位作者的时候
                    if paper_item.get("AA")[0].get("Sex") == 1:

                        for k,v in top_field_name_item.items():
                            # print("K-------------------",k)
                            field_index = FIELD_ORI_LIST.index(k)
                            #找到领域对应的下标位置后将这篇文章的数量加一
                            male_field_data[field_index][0] += male_number
                            male_field_data[field_index][1] += female_number

                            male_field_data[field_index][2] += all_male_number
                            male_field_data[field_index][3] += all_female_number

                            male_field_data[field_index][4] += h_index_number
                            male_field_data[field_index][5] += top_field_name_list_number
                            male_field_data[field_index][6] += school_list_number
                            male_field_data[field_index][7] += 1
                    else:
                        for k,v in top_field_name_item.items():
                            # print("K-------------------",k)
                            field_index = FIELD_ORI_LIST.index(k)
                            #找到领域对应的下标位置后将这篇文章的数量加一
                            female_field_data[field_index][0] += male_number
                            female_field_data[field_index][1] += female_number

                            female_field_data[field_index][2] += all_male_number
                            female_field_data[field_index][3] += all_female_number

                            female_field_data[field_index][4] += h_index_number
                            female_field_data[field_index][5] += top_field_name_list_number
                            female_field_data[field_index][6] += school_list_number
                            female_field_data[field_index][7] += 1

        print("男性~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        male_data = np.zeros((19,8))
        male_data[:,0] = male_field_data[:,0]
        male_data[:,1] = male_field_data[:,1]

        male_data[:,2] = male_field_data[:,2]
        male_data[:,3] = male_field_data[:,3]

        male_data[:,4] = male_field_data[:,4] / male_field_data[:,7]
        # male_data[:,4] = np.around(male_data[:,4],decimals=4)
        male_data[:,5] = male_field_data[:,5] / male_field_data[:,7]
        # male_data[:,5] = np.around(male_data[:,5],decimals=4)
        male_data[:,6] = male_field_data[:,6] / male_field_data[:,7]
        # male_data[:,6] = np.around(male_data[:,6],decimals=4)
        male_data[:,7] = male_field_data[:,7]




        print("处理完的数据")
        for male_data_item in male_data:
        # for male_data_item in male_field_data:
            print(male_data_item[2:])
        print(np.mean(male_data[:,0]))
        print(np.mean(male_data[:,1]))
        print(np.mean(male_data[:,2]))
        print(np.mean(male_data[:,3]))
        print(np.mean(male_data[:,4]))
        print(np.mean(male_data[:,5]))
        print(np.mean(male_data[:,6]))
        print(np.mean(male_data[:,7]))

        print("女性~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        female_data = np.zeros((19,8))
        female_data[:,0] = female_field_data[:,0]
        female_data[:,1] = female_field_data[:,1]
        female_data[:,2] = female_field_data[:,2]
        female_data[:,3] = female_field_data[:,3]
        female_data[:,4] = female_field_data[:,4] / female_field_data[:,7]
        # female_data[:,4] = np.around(female_data[:,4],decimals=4)
        female_data[:,5] = female_field_data[:,5] / female_field_data[:,7]
        # female_data[:,5] = np.around(female_data[:,5],decimals=4)
        female_data[:,6] = female_field_data[:,6] / female_field_data[:,7]
        # female_data[:,6] = np.around(female_data[:,6],decimals=4)
        female_data[:,7] = female_field_data[:,7]

        print("处理完的数据")
        for female_data_item in female_data:
        # for female_data_item in female_field_data:
            print(female_data_item[2:])
        # print(np.mean(female_data[:,0]))
        # print(np.mean(female_data[:,1]))
        print(np.mean(female_data[:,2]))
        print(np.mean(female_data[:,3]))
        print(np.mean(female_data[:,4]))
        print(np.mean(female_data[:,5]))
        print(np.mean(female_data[:,6]))
        print(np.mean(female_data[:,7]))

        # #计算论文平均被引的皮尔逊相关系数
        # male_data = list(male_data[:,4])
        # female_data = list(female_data[:,4])
        # print("论文平均被引的皮尔逊相关系数为：",pearsonr(male_data,female_data))
        #
        # #计算平均跨学科数的皮尔逊相关系数为
        male_paper_data = list(male_data[:,4])
        female_paper_data = list(female_data[:,4])

        male_field_data = list(male_data[:,5])
        female_field_data = list(female_data[:,5])

        male_school_data = list(male_data[:,6])
        female_school_data = list(female_data[:,6])

        print("论文平均被引的皮尔逊相关系数为：",pearsonr(male_paper_data,female_paper_data))
        # 0.9968663873348569

        print("计算平均跨学科数的皮尔逊相关系数为：",pearsonr(male_field_data,female_field_data))
        # 0.9819865536517202

        print("计算平均跨学校数量的皮尔逊相关系数",pearsonr(male_school_data,female_school_data))
        # 0.9660079289706027

        male_male_data = male_data[:,0] / (male_data[:,0] + male_data[:,1])
        male_female_data = male_data[:,1] / (male_data[:,0] + male_data[:,1])
        print(list(male_male_data))
        print(list(male_female_data))

        female_male_data = female_data[:,0] / (female_data[:,0] + female_data[:,1])
        female_female_data = female_data[:,1] / (female_data[:,0] + female_data[:,1])
        print(list(female_male_data))
        print(list(female_female_data))

    def male_female_person(self):
        '''
            计算male和female之间皮尔逊相关系数
        '''
        male_paper_data = [2.31,2.40,2.66,2.66,2.70,1.70,1.68,1.47,1.31,1.87,1.51,2.09,1.94,4.97,2.00,0.29,1.98,0.44,1.86]
        female_paper_data = [2.35,2.41,2.73,2.69,2.77,1.71,1.69,1.47,1.26,1.87,1.46,2.03,1.97,0.26,2.04,0.33,1.96,0.26,1.97]
        print("论文平均被引的皮尔逊相关系数为：",pearsonr(male_paper_data,female_paper_data))

        male_field_data = [4.49,5.02,4.83,4.58,6.23,4.96,4.90,5.30,6.71,6.22,7.19,7.12,4.41,7.26,5.31,6.86,6.25,8.01,6.36]
        female_field_data = [4.37,4.95,4.75,4.49,6.23,4.94,4.91,5.23,6.63,6.28,7.13,7.24,4.28,7.73,5.32,7.75,6.30,7.82,6.50]
        print("计算平均跨学科数的皮尔逊相关系数为：",pearsonr(male_field_data,female_field_data))

        male_school_data = [2.25,2.22,2.01,2.12,2.47,2.01,2.06,2.38,2.28,2.30,2.34,2.69,2.30,1.85,2.00,1.66,2.32,1.60,2.22]
        female_school_data = [2.37,2.24,2.09,2.22,2.58,2.09,2.12,2.53,2.36,2.40,2.32,2.76,2.47,1.60,2.05,1.60,2.41,1.69,2.28]
        print("计算平均跨学校数量的皮尔逊相关系数",pearsonr(male_school_data,female_school_data))

    def demo(self):
        pass
